#region File Description
//-----------------------------------------------------------------------------
// Game.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MCGameEngine.Physic_Layer;
using MCGameEngine.Sprite_Layer;
using MCGameEngine.Text_Layer;
#endregion

namespace MCGameEngine
{
    class MyGame : Microsoft.Xna.Framework.Game
    {
        public TextLayer Texts;
        public PhysicLayer Physic;
        public SpriteLayer Sprites;

        public GraphicsDeviceManager Graphics;
        public SpriteBatch spriteBatch;

        public Camera camera;

        public event EventHandler on_updating;
        public event EventHandler on_objectcollision;

        private UpdatingArgs update_args;
        private CollisionArgs collision_args;

        public MyGame()
        {
            Graphics = new GraphicsDeviceManager(this);

            camera = new Camera();

            Content.RootDirectory = "Content";

            on_updating = new EventHandler(fake_method);
            on_objectcollision = new EventHandler(fake_method);

            update_args = new UpdatingArgs();
            collision_args = new CollisionArgs();
        }

        private void fake_method(object e, EventArgs args)
        {
        
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Graphics.GraphicsDevice);

            Texts = new TextLayer(this);
            base.Components.Add(Texts);

            Physic = new PhysicLayer(this);
            base.Components.Add(Physic);

            Sprites = new SpriteLayer(this);
            base.Components.Add(Sprites);

            camera.aspectRatio = (float)this.Graphics.GraphicsDevice.Viewport.Width / this.Graphics.GraphicsDevice.Viewport.Height;

            base.LoadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            camera.Update();

            update_args.keyboardState = Keyboard.GetState();
            update_args.elapsed=elapsed;

            if (update_args.keyboardState.IsKeyDown(Keys.PageDown))
            {
                camera.Distance = camera.Distance - 20;
            }
            if (update_args.keyboardState.IsKeyDown(Keys.PageUp))
            {
                camera.Distance = camera.Distance + 20;
            }


            on_updating.Invoke("update", update_args);

            base.Update(gameTime);
        }

        public void OnObjectCollision(PhysicObject po, BasicObject bo)
        {
            collision_args.p_obj = po;
            collision_args.b_obj = bo;

            on_objectcollision.Invoke("object_collision", collision_args);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice device = this.Graphics.GraphicsDevice;
            device.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

            Physic.Draw();
            Sprites.Draw();
            Texts.Draw();

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

